package com.cskaoyan.service.wx.goodsService;

import com.cskaoyan.bean.bo.configbo.MallConfigBo;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.wxgoodsvo.FloorGoodsByWxHomeIndexVo;
import com.cskaoyan.bean.vo.wxgoodsvo.WxHomeIndexVo;
import com.cskaoyan.bean.vo.wxgoodsvo.WxMarketConfigVo;
import com.cskaoyan.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:todo
 * @Author:todo wang xv
 * @Date:todo 2022/11/22
 */
@Service
public class WxHomeServiceImpl implements WxHomeService {

    /**
     * @Description:todo 对首页商品的显示；
     * @Author:todo wang xv
     * @Date:todo 2022/11/22
     */
    @Autowired
    MarketGoodsMapper goodsMapper;

    @Autowired
    MarketCouponMapper couponMapper;

    @Autowired
    MarketCategoryMapper categoryMapper;

    @Autowired
    MarketTopicMapper topicMapper;

    @Autowired
    MarketBrandMapper brandMapper;

    @Autowired
    MarketAdMapper adMapper;

    // 配置管理；
    @Autowired
    MarketSystemMapper marketSystemMapper;

    /**
     * @Description:todo
     * @Author:todo wang xv
     * @Date:todo 2022/11/22
     */
    @Override
    public WxHomeIndexVo queryHomeIndex() {

        WxHomeIndexVo indexVo = new WxHomeIndexVo();
        // 查询新上市的商品；
        // 获取到新品上市显示的数量；
        String indexNew = "market_wx_index_new";
        String newLimit = marketSystemMapper.selectValueByName(indexNew);
        Integer newLimit2 = Integer.parseInt(newLimit);
        boolean isNew = true;
        List<MarketGoods> newGoods = goodsMapper.selectNewGoods(newLimit2, isNew);
        indexVo.setNewGoodsList(newGoods);

        // 查询所有热门商品；
        String indexHot = "market_wx_index_hot";
        String hotLimit = marketSystemMapper.selectValueByName(indexHot);
        Integer hotlimit2 = Integer.parseInt(hotLimit);
        boolean isHot = true;
        List<MarketGoods> hotGoods = goodsMapper.selectHotGoods(hotlimit2, isHot);
        indexVo.setHotGoodsList(hotGoods);

        // 查询通用优惠劵,且可以正常使用的优惠券；

        List<MarketCoupon> coupons = couponMapper.selectCoupons();
        indexVo.setCouponList(coupons);

        // 查询所有父目类；
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria2 = categoryExample.createCriteria();
        criteria2.andPidEqualTo(0);
        List<MarketCategory> categories = categoryMapper.selectByExample(categoryExample);
        indexVo.setChannel(categories);

        // 更具配置个数查询category的goods；
        // 分类栏显示数量；
        String catlog = "market_wx_catlog_list";
        String catlogLimit = marketSystemMapper.selectValueByName(catlog);
        Integer catlogLimit2 = Integer.parseInt(catlogLimit);

        String catlogGoods = "market_wx_catlog_goods";
        String logGoodsLimit = marketSystemMapper.selectValueByName(catlogGoods);
        Integer logGoodsLimit2 = Integer.parseInt(logGoodsLimit);
        List<FloorGoodsByWxHomeIndexVo> floorGoodsByWxHomeIndexVos = categoryMapper.selectcatgory(catlogLimit2);
        // 查出所有大类的id和名字
        for (FloorGoodsByWxHomeIndexVo floorGoodsByWxHomeIndexVo : floorGoodsByWxHomeIndexVos) {
            // 根据catid查询出关于本类的商品；
            Integer catid = floorGoodsByWxHomeIndexVo.getId();
            // 根据大类的id找出所有小类的id;
            List<Integer> ids = categoryMapper.selectcatgoryIds(catid);
            floorGoodsByWxHomeIndexVo.setGoodsList(goodsMapper.selectFloorGoods(logGoodsLimit2, ids));
        }
        indexVo.setFloorGoodsList(floorGoodsByWxHomeIndexVos);

        // 查询出topic;并根据配置限制个数；
        String topic = "market_wx_index_topic";
        String topicLimit = marketSystemMapper.selectValueByName(topic);
        Integer topicLimit2 = Integer.parseInt(topicLimit);
        List<MarketTopic> topics = topicMapper.selectTopic(topicLimit2);
        indexVo.setTopicList(topics);

        // 查出brandList，并根据配置限制个数；
        String brandIndex = "market_wx_index_brand";
        String brandLimit = marketSystemMapper.selectValueByName(brandIndex);
        Integer brandLimit2 = Integer.parseInt(brandLimit);
        List<MarketBrand> brands = brandMapper.selectBrand(brandLimit2);
        indexVo.setBrandList(brands);

        // 查出所有广告；
        List<MarketAd> marketAds = adMapper.selectByExample(null);
        indexVo.setBanner(marketAds);
        return indexVo;

    }

    /**
    * @Description:todo 查询商城配置
    * @Author:todo wang xv
    * @Date:todo 2022/11/24
    */
    @Override
    public WxMarketConfigVo queryMarketConfig() {
        WxMarketConfigVo marketConfigVo = new WxMarketConfigVo();
        String address = "market_mall_address";
        String latitude = "market_mall_latitude";
        String longitude = "market_mall_longitude";
        String mallName = "market_mall_name";
        String mallPhone = "market_mall_phone";
        String mallQQ = "market_mall_qq";

        marketConfigVo.setAddress(marketSystemMapper.selectValueByName(address));
        marketConfigVo.setLatitude(marketSystemMapper.selectValueByName(latitude));
        marketConfigVo.setLongitude(marketSystemMapper.selectValueByName(longitude));
        marketConfigVo.setName(marketSystemMapper.selectValueByName(mallName));
        marketConfigVo.setPhone(marketSystemMapper.selectValueByName(mallPhone));
        marketConfigVo.setQq(marketSystemMapper.selectValueByName(mallQQ));
        return marketConfigVo;

    }
}
